package top.yxqz.filter.hello;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Mr.余
 * @date 2022/12/5
 * @package top.yxqz.filter.hello
 * 拦截sayHello的请求
 * 做了中文乱码处理
 * 做了得到客户端ip的输出
 */
@WebFilter("/say")
public class SayFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

//        1.中文处理
        HttpServletResponse response  = (HttpServletResponse) servletResponse;
        response.setContentType("text/html;charset=utf-8");
//        2.得到ip地址
        HttpServletRequest request   = (HttpServletRequest) servletRequest;
        String localAddr = request.getLocalAddr();
        System.out.println("你访问了sayHello这个请求，ip是："+localAddr);
//        3.放行
        filterChain.doFilter(request,response);
    }

    @Override
    public void destroy() {

    }
}
